<?php

class Glider extends MY_SecuredController {

	public function __construct() {
		parent::MY_SecuredController();

		$this->requireRole('User');

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');

		$this->lang->load('home');
		$this->lang->load('profile/glider');
	}

	public function index($civlid) {
		$this->_preRender($civlid);

		$person = PersonModel::getPerson($civlid);

		if (!$this->_checkAdminClub($person)) {
            return false;
        }
		
		$data['person'] = $person;
		$data['pilot'] = $person->Pilot;

		$this->render('profile/glider/index', $data);
	}

	public function add($civlid) {
		$this->_preRender($civlid);

		$person = PersonModel::getPerson($civlid);
        if (!$this->_checkAdminClub($person)) {
            return false;
        }
		
		$data['person'] = $person;
		$data['pilot'] = $person->Pilot;

		if (!$this->input->post('action')) {
			$data['gliders'] = array();
			$this->_addPilotGliders($person, $data);

			$data['pilot'] = $person->Pilot;
			$this->render('profile/glider/add', $data);
		}
		else {
			$glider = Doctrine::getTable('GliderModel')->find($this->input->post('glider'));
			$data['gliders'] = array();
			$this->_addPilotGliders($person, $data);

			if ($glider) {
				$pilot = $person->Pilot;

				$index = count($pilot->Gliders) == 0? 0 : count($pilot->Gliders);

				$pilot->Gliders[$index] = $glider;
				try {
					$pilot->save();
				}
				catch (Exception $e) {
					$data['pilot'] = $pilot;

					$this->render('profile/glider/add', $data, $this->error(array(
						'message' => 'Ooops, the glider could not be added - please try again later.',
						'exception' => $e
					)));
					return;
				}

				foreach ($pilot->PilotGliderModel as $pc) {
					if ($pc->glider_id == $glider->id) {
						$pc->serial =  $this->input->post('serial');
						$pc->built = $this->input->post('built');
						$pc->tested = $this->input->post('tested');

						try {
							$pc->save();
						}
						catch (Exception $e) {
							$this->render('profile/glider/index', $data, $this->error(array(
								'message' => 'Ooops, the glider could not be added - please try again later.',
							'	exception' => $e
							)));
							return;
						}

						$this->redirect('profile/glider/index/'.$person->id, 'Glider has been successfully added!');
						break;

					}
				}

				$this->render('profile/glider/add', $data, $this->error(array(
                    'message' => "The glider could not be assigned to the pilot. Please repor this."
                    )));
			}
		}
	}

	public function edit($civlid, $gliderid) {
		$this->_preRender($civlid);

		$person = PersonModel::getPerson($civlid);
        if (!$this->_checkAdminClub($person)) {
            return false;
        }
		
		$pilot = $person->Pilot;

		if (!$this->input->post('action')) {
			$data['person'] = $person;
			$data['pilot'] = $person->Pilot;

			foreach ($pilot->Gliders as $g) {
				if ($g->id == $gliderid) {
					$data['glider'] = $g;
					$data['pilot_glider'] = $g->PilotGliderModel[0];

					$this->render('profile/glider/edit', $data);
					break;
				}
			}

			/*$data['glider'] = $person->Pilot->Gliders[$gliderid];
			 $data['pilot_glider'] = $person->Pilot->Gliders[$gliderid]->PilotGliderModel[0];
			 	
			 $this->render('profile/glider/edit', $data);*/
		}
		else {
			echo "editing record $gliderid<br/>";

			foreach ($pilot->PilotGliderModel as $pc) {
				if ($pc->glider_id == $gliderid) {
					$pc->serial =  $this->input->post('serial');
					$pc->built = $this->input->post('built');
					$pc->tested = $this->input->post('tested');

					try {
						$pc->save();
					}
					catch (Exception $e) {
						$this->render('profile/glider/index', $data, $this->error(array(
                            'message' => 'Ooops, the glider could not be added - please try again later.',
                            'exception' => $e
						)));
						return;
					}
					$this->redirect('profile/glider/index/'.$person->id, 'Glider has been successfully updated!');
					break;

				}
			}

			$this->redirect('profile/glider/index/'.$person->id, 'Glider not found?');
		}
	}

	public function delete($civlid, $gliderid = null) {
		$this->_preRender($civlid);

		$person = PersonModel::getPerson($civlid);
        if (!$this->_checkAdminClub($person)) {
            return false;
        }
		
		$data['person'] = $person;
		$data['pilot'] = $person->Pilot;

		$gliders = $person->Pilot->PilotGliderModel;
		
		if ($this->input->post('action')) {

			$data['pilot'] = $person->Pilot;

			foreach ($gliders as $g) {
				if ($this->input->post('gid_'.$g->glider_id) != "") {
					try {
						$g->delete();
					}
					catch (Exception $e) {
						$this->render('profile/glider/index', $data, $this->error(array(
							'message' => 'Ooops, one or more gliders could not be deleted.',
							'exception' => $e
						)));
						return;
					}
				}
			}

			$this->redirect('profile/glider/index/'.$person->id, 'Glider has been successfully deleted!');
		}
		else if ($gliderid != null) {
			foreach ($gliders as $g) {
				if ($g->glider_id == $gliderid) {
					try {
						$g->delete();
						$this->redirect('profile/glider/index/'.$person->id, 'Glider has been successfully deleted!');
					}
					catch (Exception $e) {
						$this->render('profile/glider/index', $data, $this->error(array(
                            'message' => 'Ooops, one or more gliders could not be deleted.',
                            'exception' => $e
						)));
						return;
					}
				}
			}
				
			$this->render('profile/glider/index', $data, $this->error(array(
                'message' => "Glider '$gliderid' could not be found!"
			)));
		}
	}

	private function _addPilotGliders(&$person, &$data) {
		$gliders = GliderModel::findAll(); //Doctrine::getTable('GliderModel')->findAll();
		$data['gliders'] = array();
			
		$pilotGliders = array();
		foreach ($person->Pilot->Gliders as $pc) {
			$pilotGliders[] = $pc->id;
		}

		foreach ($gliders as $g) {
			if (!in_array($g->id, $pilotGliders, true)) {
				$data['gliders'][$g->id] = GliderModel::getGliderBrand($g->brand).' - '.$g->model;

				if ($g->proto) {
					$data['gliders'][$g->id] = $g->brand.' - '.$g->model.' ('.lang('profile.glider.Proto').')';
				}
			}
		}
	}
}
?>
